import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mainColor: app.globalData.mainColor,
    baseImgUrl: app.globalData.baseImgUrl,
    headImg: '',
    nickName: '',
    signDesc: '',
    editShow: false,
    overlay: false,
  },

  //获取用户信息
  getUserInfo() {
    let that = this;
    // 微信登录
    wx.login({
      success: res => {
        //   console.log(JSON.stringify('login-res:'+JSON.stringify(res)))
        //   console.log(JSON.stringify('login-code:'+res.code))
        //   console.log("app-baseUrl:",this.globalData.baseUrl)
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        // app.http.get('/gd-fishing/avoid/xcx/authorize/' + res.code).then((res) => {
        //   // console.log('http-res:', JSON.stringify(res))
        //   if (app.errorAlert(res)) {
        //     return;
        //   }
        //   wx.setStorageSync('token', res.data.token);
        //   //获取系統用户数据
        //   that.getUserData();
        // })
        let result = {
          "code": 1,
          "msg": "成功",
          "data": {
            "token": "49fe9f8cdc1e1301dba4700206f07f7b",
            "registerState": 1
          },
          "remarks": null
        };
        wx.setStorageSync('token', result.data.token);
        //获取系統用户数据
        that.getUserData();
      }
    })
  },

  //获取系統用户数据
  getUserData: function () {
    let that = this;
    //拿到token获取用户信息
    // app.http.get('/gd-fishing/user/info').then((res) => {
    //   // console.log('http-res:', JSON.stringify(res));
    //   let data = "";
    //   if (res.code == 1) {
    //     data = res.data;
    //   }
    //   wx.setStorageSync('userInfo', data);
    //   that.setData({
    //     nickName: data.nickName,
    //     headImg: data.headImg,
    //     signDesc: data.signDesc,
    //   })
    //   // console.log('userInfo:', wx.getStorageSync('userInfo'));
    // })
    // console.log('http-res:', JSON.stringify(res));
    let res = {
      "code": 1,
      "msg": "成功",
      "data": {
        "userId": 8,
        "userPhone": null,
        "nickName": "优雅的空军佬",
        "headImg": "/20241029/1730191347233.jpeg",
        "signDesc": "路虽远，行则将至！",
        "userSex": null
      },
      "remarks": null
    };
    let data = "";
    if (res.code == 1) {
      data = res.data;
    }
    wx.setStorageSync('userInfo', data);
    that.setData({
      nickName: data.nickName,
      headImg: data.headImg,
      signDesc: data.signDesc,
    })
    // console.log('userInfo:', wx.getStorageSync('userInfo'));
  },

  //刷新数据
  refreshData: function () {
    // console.log('-->refresh!');
    let userInfo = wx.getStorageSync('userInfo');
    let logout = userInfo == null || userInfo == undefined || userInfo == '';
    this.setData({
      nickName: logout ? '' : userInfo.nickName,
      headImg: logout ? '' : userInfo.headImg,
      signDesc: logout ? '' : userInfo.signDesc,
    });
  },

  //选择头像(获取微信头像)
  checkImg(e) {
    let that = this;
    // console.log("-->e:", e);
    // console.log("-->img:", e.detail.avatarUrl);
    that.setData({
      overlay: true
    });
    wx.uploadFile({
      url: app.globalData.baseUrl + '/gd-fishing/upload/img',
      filePath: e.detail.avatarUrl,
      header: {
        'Authorization': wx.getStorageSync('token')
      },
      name: 'file',
      formData: {},
      success(res) {
        that.setData({
          overlay: false
        });
        // console.log('http-res:', JSON.stringify(res.data));
        let jsonData = JSON.parse(res.data);
        // console.log('http-res:', jsonData.data);
        if (jsonData.code == 1) {
          that.setData({
            "headImg": jsonData.data
          });
          that.keep();
        }
      },
    });
  },

  //修改信息
  editInfo() {
    let that = this;
    that.keep();
    that.closePopup();
  },
  showPopup() {
    this.setData({
      "editShow": true
    });
  },
  closePopup() {
    this.refreshData();
    this.setData({
      "editShow": false
    });
  },

  //保存
  keep() {
    let that = this;
    let data = {
      "headImg": that.data.headImg,
      "nickName": that.data.nickName,
      "signDesc": that.data.signDesc,
    };
    app.http.put('/gd-fishing/user', data).then(res => {
      // console.log('http-result:', JSON.stringify(res));
      if (app.errorAlert(res)) {
        return;
      }
      wx.setStorageSync('userInfo', data);
      // console.log('update-->userInfo:', wx.getStorageSync('userInfo'));
      that.refreshData();
    });
  },

  //跳转意见反馈页面
  toFeedback: function () {
    wx.navigateTo({
      url: '/pages/mine/feedback/index'
    })
  },

  //退出登录
  logout: function () {
    //前一页
    let that = this;
    Dialog.confirm({
      message: '确定退出登录吗？',
    }).then(() => {
      wx.setStorageSync('userInfo', '');
      that.setData({
        nickName: '',
        headImg: '',
        signDesc: ''
      });
      // app.http.get('/gd-fishing/user/logout').then((res) => {
      //   // console.log('http-res:', JSON.stringify(res));
      // })
    }).catch(() => {});
  },

  //跳转动态
  toDynamic(e) {
    // console.log("------>val:", e.currentTarget.dataset.val);
    if (app.loginAlert(true)) {
      Toast('请点击登录~');
      return;
    }
    wx.navigateTo({
      url: '/pages/mine/dynamic/index'
    })
  },

  //跳转收藏
  toCollect(e) {
    // console.log("------>val:", e.currentTarget.dataset.val);
    if (app.loginAlert(true)) {
      Toast('请点击登录~');
      return;
    }
    wx.navigateTo({
      url: '/pages/mine/collect/index'
    })
  },

  //跳转关注
  toConcern(e) {
    // console.log("------>val:", e.currentTarget.dataset.val);
    if (app.loginAlert(true)) {
      Toast('请点击登录~');
      return;
    }
    wx.navigateTo({
      url: '/pages/mine/concern/index'
    })
  },

  //跳转足迹
  toHistory(e) {
    // console.log("------>val:", e.currentTarget.dataset.val);
    app.notOpen();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.refreshData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    this.getUserData();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})